#include <QLineEdit>
#include "ItemString.h"

QString ItemString::get()
{
    return m_data;
}

void ItemString::set(const QString &value)
{
    m_data = value;
    emit itemEdited();
}

QWidget *ItemString::createWidget(WidgetStyle s)
{
    Q_UNUSED(s)
    auto w = new QLineEdit;
    connect(w, &QLineEdit::textEdited, this, &ItemString::onWidgetEditedWrapper);
    return w;
}

void ItemString::update(QWidget *widget, UpdateOption option)
{
    QLineEdit *lineEdit = qobject_cast<QLineEdit *>(widget);
    if (lineEdit != nullptr)
    {
        if (option == UpdateOption::WidgetContent)
        {
            lineEdit->setText(m_data);
        }
        else if (option == UpdateOption::Item)
        {
            this->m_data = lineEdit->text();
            qInfo() << "update: " << this->m_data;
        }
    }
    else
    {
        qWarning() << "";
    }
}

void ItemString::writeToData(QDataStream &stream) const
{
    stream << m_data;
}

void ItemString::readFromData(QDataStream &stream)
{
    stream >> m_data;
    emit itemEdited();
}
